package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.MaltNotes;

public class MaltNotesPresenter extends AbstractPresenter<MaltNotes> {
	final String SINGLEITEM = "maltNotes";
	final String MULTIITEM = "maltNotess";
	
	public ArrayList<MaltNotes> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public MaltNotes tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public MaltNotes createItemFromJSONObject(JSONObject jsonObject){
		MaltNotes item = new MaltNotes();
		try{
			item.setId(jsonObject.getInt("maltNoteID"));
			item.setLbs(jsonObject.getDouble("maltNoteLbs"));
			
			MaltPresenter presenter = new MaltPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("maltID");
			item.setMalt(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(MaltNotes item){
		try{
			JSONObject json = new JSONObject();
			json.put("maltNoteID", item.getId());
			json.put("maltNoteLbs", item.getLbs());

			MaltPresenter presenter = new MaltPresenter();
			json.put("maltID", presenter.createJSON(item.getMalt()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public MaltNotes getEmptyEntity(){
		MaltNotes entity = new MaltNotes();
		return entity;
	}
}
